package com.heisen.security.handler;

import com.heisen.base.entity.Api;
import com.heisen.base.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class XmAccessDeniedHandler implements AccessDeniedHandler {


    /**
     * 用户无权限返回403
     * @param request that resulted in an <code>AccessDeniedException</code>
     * @param response so that the user agent can be advised of the failure
     * @param accessDeniedException that caused the invocation
     *
     * @throws IOException
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws  IOException {

        log.info("【XmAccessDeniedHandler     403   没有权限访问该资源】");
        ResponseUtils.makeResponse(response,MediaType.APPLICATION_JSON_UTF8_VALUE,HttpServletResponse.SC_FORBIDDEN,Api.fail("没有权限访问该资源"));
    }
}